﻿using System;
using System.Collections.Generic;
using System.Text;
using Belmost4PDA.DatabaseWorkPDA;

namespace Belmost4PDA.Fasade
{
    public class DefectsReader
    {

        public class DefItem
        {
            public string defCode;
            public bool isRadio;
            public int clsDefUn;
            public string clsShort;
            public string defNm;
            public string rootDef;
            public string defNPP;
            public bool isDefault;
        }
        public class brDefItem
        {
            public string defCode;
            public string idBrDef;
            public string defScope;
            public bool defOn;
            public bool isHave;
        }
        private const string deft = "select defCode, rootDef, isRadio, defNPP, defNm, clsDefUn.clsDefUn, clsShort, isDefault from defbm join defKindBm on defbm.iddefkind = defkindbm.iddefkind join clsDefun on defkindbm.clsdefun = clsDefUn.clsDefUn order by defkindbm.defNpp";
        public static List<DefItem> GetDefItems()
        {
            QuerySelect q =new QuerySelect();
            q.Select(deft);
            List<DefItem> defectlist = new List<DefItem>();
            List<DataRows.DataRows> lst = q.GetRows();
            foreach (DataRows.DataRows rows in lst)
            {
                DefItem defect=new DefItem();
                defect.defCode = rows.FieldByName("defCode");
                defect.isRadio = (rows.FieldByName("isRadio")=="1");
                defect.clsDefUn = Convert.ToInt32(rows.FieldByName("clsDefUn"));
                defect.clsShort = rows.FieldByName("clsShort");
                defect.defNm = rows.FieldByName("defNm");
                defect.rootDef = rows.FieldByName("rootDef");
                defect.defNPP = rows.FieldByName("defNPP");
                defect.isDefault = (rows.FieldByName("isDefault")=="1");
                defectlist.Add(defect);
            }
            return defectlist;
        }
        private const string defit = "select defCode, idBrDef, defScope, defOn, isHave from BrDef where idBr = {0} and idBrElm = {1} order by defcode";/*idbrdef";*/
        public static List<brDefItem> GetBrDefItems(int idElem, int idBr)
        {
            QuerySelect q = new QuerySelect();
            q.Select(String.Format(defit, idBr, idElem));
            List<brDefItem> brdefitemlist =new List<brDefItem>();
            List<DataRows.DataRows> lst = q.GetRows();
            foreach (DataRows.DataRows rows in lst)
            {
                brDefItem defitem=new brDefItem();
                defitem.defCode = rows.FieldByName("defCode");
                defitem.idBrDef = rows.FieldByName("idBrDef");
                defitem.defScope = rows.FieldByName("defScope");
                defitem.defOn = (rows.FieldByName("defOn")=="1");
                defitem.isHave = (rows.FieldByName("isHave")=="1");
                brdefitemlist.Add(defitem);
            }
            return brdefitemlist;
        }
        private const string upd = "update BrDef set defScope = {0}, defOn = {1} where idBrDef = {2}";
        public static string UpdateDef(string def, bool isChecked, string value)
        {
            QueryExec q = new QueryExec();
            string SQL = String.Format(upd, value, isChecked ? "1" : "0", def);
            q.Execute(SQL);
            return SQL;
            
        }
    }
}
